package com.bana.fvs.utils;

import com.alibaba.fastjson.JSON;
import com.bana.fvs.code.SystemLogType;
import com.bana.fvs.model.FogArea;
import com.bana.sys.service.LogService;
import com.bana.sys.utils.Token;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 通知雾区方案websocket
 */
@ServerEndpoint(value = "/websocket/fogArea1")
@Component
public class FogAreaServer_out {

    /**
     * 客户端集合
     */
    private static CopyOnWriteArraySet<FogAreaServer_out> webSocketSet = new CopyOnWriteArraySet<>();

    /**
     * 发送session
     */
    private Session session;


    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        webSocketSet.add(this);
    }

    @OnClose
    public void onClose() {
        webSocketSet.remove(this);
    }

    /**
     * 发送到客户端
     * @param json 发送内容
     */
    private void send(String json)  {

        try
        {
            this.session.getBasicRemote().sendText(json);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    /**
     * 发送到全部客户端
     * @param fogArea 雾区信息
     */
    public static void sendAll(FogArea fogArea){

        String json= JSON.toJSONString(com.bana.out.model.FogArea.Convert(fogArea));
        for (FogAreaServer_out server : webSocketSet) {
            server.send(json);
        }


    }

}
